package com.bnp.reporting.java8.collection;

import com.bnp.reporting.java8.entity.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalDouble;

/**
 * Created by adib on 11/11/14.
 */
public class SumAndAverage {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();

        people.add(new Person(34, "Adib"));
        people.add(new Person(30, "Neeraj"));
        people.add(new Person(37, "HongFei"));

        int sum = people.stream()
                    .mapToInt(p -> p.getAge())
                    .sum();
        System.out.println("Total of ages: " + sum);

        OptionalDouble average = people.stream()
                                    .mapToInt(p -> p.getAge())
                                    .average();
        if(average.isPresent()){
            System.out.println("Average: " + average.getAsDouble());
        }
        else{
            System.out.println("Average is not available.");
        }

    }
}
